package com.ruoyi.zhgd.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.dto.RoleDTO;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.zhgd.mapper.ZjHomeMapper;
import com.ruoyi.zhgd.service.IZjHomeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 首页Service业务层处理
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjHomeServiceImpl implements IZjHomeService {

    private final ZjHomeMapper baseMapper;

    /**
     * 首页接入设备及预警统计
     *
     * @return
     */
    @Override
    public R<List<Map<String,Object>>> count(Map<String, Object> params) {
        return R.ok(baseMapper.count());
    }

    /**
     * 首页-拌合站今日生产统计
     *
     * @return
     */
    @Override
    public R<List<Map<String, Object>>> productToday(Map<String, Object> params) {
        if (ObjectUtils.isEmpty(params.get("deptId"))) {
            throw new ServiceException("查询部门不能为空");
        }
        // 根据部门id查询子集
        List<String> deptIdList = baseMapper.getSubDeptList(params.get("deptId").toString());
        // 混凝土查询
        List<Map<String, Object>> hntData = baseMapper.productToday(deptIdList);
        // 水稳查询  @TODO
        // 沥青查询 @TODO

        for (Map<String, Object> hntDatum : hntData) {
            hntDatum.put("swNum",0); // @TODO 水稳暂时设置为0
            hntDatum.put("lqNum",0); // @TODO 沥青暂时设置为0
        }
        return R.ok(hntData);
    }

    /**
     * 首页-拌合站产能统计
     *
     * @param params
     * @return
     */
    @Override
    public R<List<List<Object>>> productYear(Map<String, Object> params) {
        if (ObjectUtils.isEmpty(params.get("deptId"))) {
            throw new ServiceException("查询部门不能为空");
        }
        if(ObjectUtils.isEmpty(params.get("type")) && ObjectUtils.isEmpty(params.get("dateType"))) {
            throw new ServiceException("参数不能为空");
        }
        // 根据部门id查询子集
        List<String> deptIdList = baseMapper.getSubDeptList(params.get("deptId").toString());
        List<List<Object>> data = new ArrayList<>();
        List<Map<String, Object>> data0 = new ArrayList<>();
        List<List<Map<String, Object>>> data00 = new ArrayList<>();
//        List<Map<String, Object>> data1 = null; // 一标
//        List<Map<String, Object>> data2 = null; // 二标
//        List<Map<String, Object>> data3 = null; // 三标
//        List<Map<String, Object>> data4 = null; // 四标
        if ("1".equals(params.get("type"))) { // 水稳 @TODO

        } else if ("2".equals(params.get("type"))) { // 沥青 @TODO

        } else { // 混凝土
            if("week".equals(params.get("dateType"))) { // 按周查询
                for (String deptId : deptIdList) {
                    data00.add(baseMapper.productWeek0(deptId));
                }
//                data1 = baseMapper.productWeek1(); // 一标
//                data2 = baseMapper.productWeek2(); // 二标
//                data3 = baseMapper.productWeek3(); // 三标
//                data4 = baseMapper.productWeek4(); // 四标
            } else if("month".equals(params.get("dateType"))) { // 按月查询
                for (String deptId : deptIdList) {
                    data00.add(baseMapper.productMonth0(deptId));
                }
//                data1 = baseMapper.productMonth1(); // 一标
//                data2 = baseMapper.productMonth2(); // 二标
//                data3 = baseMapper.productMonth3(); // 三标
//                data4 = baseMapper.productMonth4(); // 四标
            } else if("year".equals(params.get("dateType"))) { // 按年查询
                for (String deptId : deptIdList) {
                    data00.add(baseMapper.productYear0(deptId));
                }
//                data1 = baseMapper.productYear1(); // 一标
//                data2 = baseMapper.productYear2(); // 二标
//                data3 = baseMapper.productYear3(); // 三标
//                data4 = baseMapper.productYear4(); // 四标
            } else if("total".equals(params.get("dateType"))) { // 累计总量
                for (String deptId : deptIdList) {
                    data00.add(baseMapper.productTotal0(deptId));
                }
            }
            // 拼成前端展示的数据格式
//            List<Object> names = new ArrayList<>();
//            names.add(data1.get(0).get("name"));
//            names.add(data2.get(0).get("name"));
//            names.add(data3.get(0).get("name"));
//            names.add(data4.get(0).get("name"));
//            data.add(names);
//            for (int i=0; i<data1.size(); i++) {
//                List<Object> res = new ArrayList<>();
//                res.add(data1.get(i).get("dateTime"));
//                res.add(data1.get(i).get("hntNum"));
//                res.add(data2.get(i).get("hntNum"));
//                res.add(data3.get(i).get("hntNum"));
//                res.add(data4.get(i).get("hntNum"));
//                data.add(res);
//            }
            List<Object> names = new ArrayList<>();
            List<Object> dateTime = new ArrayList<>();
            List<List<Object>> hntNums = new ArrayList<>();
            for (int i=0; i<data00.size(); i++) {
                if (ObjectUtils.isEmpty(data00.get(i)) || ObjectUtils.isEmpty(data00.get(i).get(0))) {
                    continue;
                }
                names.add(data00.get(i).get(0).get("name"));
                dateTime = data00.get(i).stream().map(m -> m.get("dateTime")).collect(Collectors.toList());
                List<Object> hntNum = data00.get(i).stream().map(s -> s.get("hntNum")).collect(Collectors.toList());
                hntNums.add(hntNum);
            }
            data.add(names);
            for (int i=0; i<dateTime.size(); i++) {
                List<Object> bus = new ArrayList<>();
                bus.add(dateTime.get(i));
                for (int j=0; j<hntNums.size(); j++) {
                    bus.add(hntNums.get(j).get(i));
                }
                data.add(bus);
            }
        }
        return R.ok(data);
    }

    /**
     * 各标段张拉梁片次数对比
     * @return
     */
    @Override
    public R<List<Map<String, Object>>> tensionBeamNoCount(Map<String, Object> params) {
        if (ObjectUtils.isEmpty(params.get("deptId"))) {
            throw new ServiceException("查询部门不能为空");
        }
        // 根据部门id查询子集
        List<String> deptIdList = baseMapper.getSubDeptList(params.get("deptId").toString());
        return R.ok(baseMapper.tensionBeamNoCount0(deptIdList));
    }

    /**
     * 各标段实验室设备在线统计
     *
     * @return
     */
    @Override
    public R<List<Map<String, Object>>> labDeviceOnlineCount(Map<String, Object> params) {
        if (ObjectUtils.isEmpty(params.get("deptId"))) {
            throw new ServiceException("查询部门不能为空");
        }
        // 根据部门id查询子集
        List<String> deptIdList = baseMapper.getSubDeptList(params.get("deptId").toString());
        // 各标实验室设备在线数
        List<Map<String, Object>> nums = baseMapper.labDeviceOnlineNumCount0(deptIdList);
        // 各标段实验室设备在线当月在线累计时长
        List<Map<String, Object>> times = baseMapper.labDeviceOnlineTimeCount0(deptIdList);

        List<Map<String, Object>> data = new ArrayList<>();
        // 各标实验室设备在线数
//        List<Map<String, Object>> nums = baseMapper.labDeviceOnlineNumCount();
        // 各标段实验室设备在线当月在线累计时长
//        List<Map<String, Object>> times = baseMapper.labDeviceOnlineTimeCount();
        for (int i=0; i<nums.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            Object name = nums.get(i).get("name");
            Object code = nums.get(i).get("code");
            Long sum = (Long)nums.get(i).get("sum");
            BigDecimal onlineNum = (BigDecimal)nums.get(i).get("onlineNum");
            BigDecimal offlineNum = (BigDecimal)nums.get(i).get("offlineNum");
            Object bootDuration = times.get(i).get("bootDuration");
            map.put("name", name);
            map.put("sum", sum);
            map.put("onlineNum", onlineNum);
            map.put("offlineNum", offlineNum);
            map.put("bootDuration", bootDuration);
            data.add(map);
        }
        return R.ok(data);
    }

    /**
     * 各标段拌合站设备在线统计
     *
     * @return
     */
    @Override
    public R<List<Map<String, Object>>> mixDeviceOnlineCount(Map<String, Object> params) {
        if (ObjectUtils.isEmpty(params.get("deptId"))) {
            throw new ServiceException("查询部门不能为空");
        }
        // 根据部门id查询子集
        List<String> deptIdList = baseMapper.getSubDeptList(params.get("deptId").toString());
        // 各标拌合站设备在线数
        List<Map<String, Object>> nums = baseMapper.mixDeviceOnlineNumCount0(deptIdList);
        // 各标段拌合站设备在线当月在线累计时长
        List<Map<String, Object>> times = baseMapper.mixDeviceOnlineTimeCount0(deptIdList);
        List<Map<String, Object>> data = new ArrayList<>();
        // 各标拌合站设备在线数
//        List<Map<String, Object>> nums = baseMapper.mixDeviceOnlineNumCount();
        // 各标段拌合站设备在线当月在线累计时长
//        List<Map<String, Object>> times = baseMapper.mixDeviceOnlineTimeCount();
        for (int i=0; i<nums.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            Object name = nums.get(i).get("name");
            Object code = nums.get(i).get("code");
            Long sum = (Long)nums.get(i).get("sum");
            BigDecimal onlineNum = (BigDecimal)nums.get(i).get("onlineNum");
            BigDecimal offlineNum = (BigDecimal)nums.get(i).get("offlineNum");
            Object bootDuration = times.get(i).get("bootDuration");
            map.put("name", name);
            map.put("sum", sum);
            map.put("onlineNum", onlineNum);
            map.put("offlineNum", offlineNum);
            map.put("bootDuration", bootDuration);
            data.add(map);
        }
        return R.ok(data);
    }

    /**
     * 各标段实验次数与不合格次数对比分析
     *
     * @param params
     * @return
     */
    @Override
    public R<List<List<Object>>> labEligibleCount(Map<String, Object> params) {
        if (ObjectUtils.isEmpty(params.get("eligible"))) {
            throw new ServiceException("参数不能为空");
        }
        List<List<Object>> data = new ArrayList<>();
        List<Map<String, Object>> data1 = null; // 一标试验数据
        List<Map<String, Object>> data2 = null; // 二标试验数据
        List<Map<String, Object>> data3 = null; // 三标试验数据
        List<Map<String, Object>> data4 = null; // 四标试验数据
        if ("1".equals(params.get("eligible"))) {
            data1 = baseMapper.labEligibleCountY1();
            data2 = baseMapper.labEligibleCountY2();
            data3 = baseMapper.labEligibleCountY3();
            data4 = baseMapper.labEligibleCountY4();
        } else {
            data1 = baseMapper.labEligibleCountN1();
            data2 = baseMapper.labEligibleCountN2();
            data3 = baseMapper.labEligibleCountN3();
            data4 = baseMapper.labEligibleCountN4();
        }
        // 拼成前端展示的数据格式
        List<Object> names = new ArrayList<>();
        names.add(data1.get(0).get("name"));
        names.add(data2.get(0).get("name"));
        names.add(data3.get(0).get("name"));
        names.add(data4.get(0).get("name"));
        data.add(names);
        for (int i=0; i<data1.size(); i++) {
            List<Object> res = new ArrayList<>();
            res.add(data1.get(i).get("typeName"));
            res.add(data1.get(i).get("num"));
            res.add(data2.get(i).get("num"));
            res.add(data3.get(i).get("num"));
            res.add(data4.get(i).get("num"));
            data.add(res);
        }
        return R.ok(data);
    }

    @Override
    public R selectLabEligibleCount(Map<String, Object> params) {
        if (ObjectUtils.isEmpty(params.get("deptId"))) {
            throw new ServiceException("查询部门不能为空");
        }
        // 根据部门id查询子集
        List<String> deptIdList = baseMapper.getSubDeptList(params.get("deptId").toString());
        List<Map<String, Object>> list = new ArrayList<>();
        for (String deptId : deptIdList) {
            List<Map<String, Object>> list1 = baseMapper.selectLabList(deptId);
            Map<String, Object> map = new HashMap();
            map.put("name",list1.get(0).get("name"));
            map.put("zrd1", list1.get(0).get("num"));
            map.put("zrd2", list1.get(0).get("elNum"));
            map.put("rhd1", list1.get(1).get("num"));
            map.put("rhd2", list1.get(1).get("elNum"));
            map.put("yd1", list1.get(2).get("num"));
            map.put("yd2", list1.get(2).get("elNum"));
            map.put("mxr1", list1.get(3).get("num"));
            map.put("mxr2", list1.get(3).get("elNum"));
            map.put("rsl1", list1.get(4).get("num"));
            map.put("rsl2", list1.get(4).get("elNum"));
            map.put("ylj1", list1.get(5).get("num"));
            map.put("ylj2", list1.get(5).get("elNum"));
            map.put("wnj1", list1.get(6).get("num"));
            map.put("wnj2", list1.get(6).get("elNum"));
            list.add(map);
        }
//        // 一标试验数据
//        List<Map<String, Object>> list1 = baseMapper.selectLabList1();
//        // 二标试验数据
//        List<Map<String, Object>> list2 = baseMapper.selectLabList2();
//        // 三标试验数据
//        List<Map<String, Object>> list3 = baseMapper.selectLabList3();
//        // 四标试验数据
//        List<Map<String, Object>> list4 = baseMapper.selectLabList4();
//
//        Map<String, Object> map1 = new HashMap();
//        map1.put("name",list1.get(0).get("name"));
//        map1.put("zrd1", list1.get(0).get("num"));
//        map1.put("zrd2", list1.get(0).get("elNum"));
//        map1.put("rhd1", list1.get(1).get("num"));
//        map1.put("rhd2", list1.get(1).get("elNum"));
//        map1.put("yd1", list1.get(2).get("num"));
//        map1.put("yd2", list1.get(2).get("elNum"));
//        map1.put("mxr1", list1.get(3).get("num"));
//        map1.put("mxr2", list1.get(3).get("elNum"));
//        map1.put("rsl1", list1.get(4).get("num"));
//        map1.put("rsl2", list1.get(4).get("elNum"));
//        map1.put("ylj1", list1.get(5).get("num"));
//        map1.put("ylj2", list1.get(5).get("elNum"));
//        map1.put("wnj1", list1.get(6).get("num"));
//        map1.put("wnj2", list1.get(6).get("elNum"));
//
//        Map<String, Object> map2 = new HashMap();
//        map2.put("name",list2.get(0).get("name"));
//        map2.put("zrd1", list2.get(0).get("num"));
//        map2.put("zrd2", list2.get(0).get("elNum"));
//        map2.put("rhd1", list2.get(1).get("elNum"));
//        map2.put("rhd2", list2.get(1).get("elNum"));
//        map2.put("yd1", list2.get(2).get("num"));
//        map2.put("yd2", list2.get(2).get("elNum"));
//        map2.put("mxr1", list2.get(3).get("num"));
//        map2.put("mxr2", list2.get(3).get("elNum"));
//        map2.put("rsl1", list2.get(4).get("num"));
//        map2.put("rsl2", list2.get(4).get("elNum"));
//        map2.put("ylj1", list2.get(5).get("num"));
//        map2.put("ylj2", list2.get(5).get("elNum"));
//        map2.put("wnj1", list2.get(6).get("num"));
//        map2.put("wnj2", list2.get(6).get("elNum"));
//
//        Map<String, Object> map3 = new HashMap();
//        map3.put("name",list3.get(0).get("name"));
//        map3.put("zrd1", list3.get(0).get("num"));
//        map3.put("zrd2", list3.get(0).get("elNum"));
//        map3.put("rhd1", list3.get(1).get("num"));
//        map3.put("rhd2", list3.get(1).get("elNum"));
//        map3.put("yd1", list3.get(2).get("num"));
//        map3.put("yd2", list3.get(2).get("elNum"));
//        map3.put("mxr1", list3.get(3).get("num"));
//        map3.put("mxr2", list3.get(3).get("elNum"));
//        map3.put("rsl1", list3.get(4).get("num"));
//        map3.put("rsl2", list3.get(4).get("elNum"));
//        map3.put("ylj1", list3.get(5).get("num"));
//        map3.put("ylj2", list3.get(5).get("elNum"));
//        map3.put("wnj1", list3.get(6).get("num"));
//        map3.put("wnj2", list3.get(6).get("elNum"));
//
//        Map<String, Object> map4 = new HashMap();
//        map4.put("name",list4.get(0).get("name"));
//        map4.put("zrd1", list4.get(0).get("num"));
//        map4.put("zrd2", list4.get(0).get("elNum"));
//        map4.put("rhd1", list4.get(1).get("num"));
//        map4.put("rhd2", list4.get(1).get("elNum"));
//        map4.put("yd1", list4.get(2).get("num"));
//        map4.put("yd2", list4.get(2).get("elNum"));
//        map4.put("mxr1", list4.get(3).get("num"));
//        map4.put("mxr2", list4.get(3).get("elNum"));
//        map4.put("rsl1", list4.get(4).get("num"));
//        map4.put("rsl2", list4.get(4).get("elNum"));
//        map4.put("ylj1", list4.get(5).get("num"));
//        map4.put("ylj2", list4.get(5).get("elNum"));
//        map4.put("wnj1", list4.get(6).get("num"));
//        map4.put("wnj2", list4.get(6).get("elNum"));

//        list.add(map1);
//        list.add(map2);
//        list.add(map3);
//        list.add(map4);

        return R.ok(list);
    }

    /**
     * 新首页-接入设备及预警统计
     *
     * @return
     */
    @Override
    public R<List<Map<String, Object>>> newCount(Map<String, Object> params) {
        if (ObjectUtils.isEmpty(params.get("deptId"))) {
            throw new ServiceException("查询部门不能为空");
        }
        // 根据部门id查询子集
        List<String> deptIdList = baseMapper.getSubDeptList(params.get("deptId").toString());

        String nameLab = "试验室设备";
        List<Map<String,Object>> data1 = baseMapper.newCountLab(deptIdList, nameLab);
        String nameMix = "拌合站设备";
        List<Map<String,Object>> data2 = baseMapper.newCountMix(deptIdList, nameMix);
        String nameTension = "张拉设备";
        List<Map<String,Object>> data3 = baseMapper.newCountTension(deptIdList, nameTension);
        String nameGrouting = "压浆设备";
        List<Map<String,Object>> data4 = baseMapper.newCountGrouting(deptIdList, nameGrouting);

        List<Map<String, Object>> data = new ArrayList<>();

        Map<String, Object> map1 = new HashMap<>();
        map1.put("name", nameLab);
        map1.put("total", data1.get(0).get("sum"));
        data1.remove(0);
        map1.put("list", data1);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("name", nameMix);
        map2.put("total", data2.get(0).get("sum"));
        data2.remove(0);
        map2.put("list", data2);


        Map<String, Object> map3 = new HashMap<>();
        map3.put("name", nameTension);
        map3.put("total", data3.get(0).get("sum"));
        data3.remove(0);
        map3.put("list", data3);


        Map<String, Object> map4 = new HashMap<>();
        map4.put("name", nameGrouting);
        map4.put("total", data4.get(0).get("sum"));
        data4.remove(0);
        map4.put("list", data4);

        data.add(map1);
        data.add(map2);
        data.add(map3);
        data.add(map4);

        return R.ok(data);
    }

    /**
     * 待办消息通知数量统计
     * @return
     */
    @Override
    public R<List<Map<String, Object>>> todoCount() {
        List<Map<String, Object>> data = new ArrayList<>();
        Integer status; // 返回页面的数据列表状态
        List<Integer> res; // 返回页面的数据列表
        Integer disposalStatus; // 处置状态值
        // 获取当前用户角色信息,如果是监理,则为待审批,如果非监理,则为未处置
        Boolean contains = judgeIsJl(LoginHelper.getLoginUser().getRoles());
        // 获取未处置或处置中的 张拉 压浆 试验 混凝土 水稳 沥青 的预警通知数量
        Map<String, Object> statusMap = new HashMap<>();
        Map<String, Object> countMap = new HashMap<>();
        if (contains) { // 监理 处置中
            status = 3;
            disposalStatus = 3;
            res = baseMapper.tudoDisposal(disposalStatus, LoginHelper.getUserId());
        } else { // 非监理 未处置
            status = 1;
            disposalStatus = 1;
            res = baseMapper.todoCount(disposalStatus, LoginHelper.getDeptId());
        }
        int total = res.stream().mapToInt(Integer::intValue).sum();
        statusMap.put("status", status);
        countMap.put("count", res);
        countMap.put("total", total);
        data.add(statusMap);
        data.add(countMap);
        return R.ok(data);
    }

    // 监理角色
    public final String[] jlArr = {"jw.1","jw.2","jw.3","jw.4"};

    /**
     *  判断是否监理角色
     * @param roles
     * @return
     */
    public Boolean judgeIsJl(List<RoleDTO> roles) {
        Boolean contains = false;
        if (CollectionUtils.isNotEmpty(roles)) {
            for (RoleDTO role : roles) {
                contains = Arrays.asList(jlArr).contains(role.getRoleKey());
            }
        }
        return contains;
    }
}
